#!/usr/bin/perl -w
# REDS2PSAT converts REDS data file into PSAT data file
#
# REDS2PSAT <OPTIONS> FILEINPUT <FILEOUTPUT>
#
# Author:  Federico Milano
# Date:    07-May-2009
# Version: 1.0.0
#
# E-mail:  federico.milano@ucd.ie
#

use strict;

# -----------------------------------------------------------------------
# variable declaration
# -----------------------------------------------------------------------
my $nargin = 0;
my $verbose = 0;
my $helpmsg = 0;
my ($i,$h,$i1,$i2,$i3,$i4);

my $npq = -1;
my $nline = -1;

my $mva = 100.0;
my $kva = 1000000.0;
my $vn = 20.0;
my $yb = 0.25;

my $format;
my @data;

my $pu = 0;
my $branch_count = 0;
my $sec_count = 0;
my $tie_count = 0;
my $nodes = 0;
my $totp = 0;
my $feeders = 0;
my @feeders_ids;
my $slack = 1;
my $tie = 1;
my (@busfr, @bustt, @r, @x, @p, @q, @buspq);
my ($p, $q);

# -----------------------------------------------------------------------
# check inputs
# -----------------------------------------------------------------------
$nargin = @ARGV;
$nargin || die "Error: No input data file.\n";

# -----------------------------------------------------------------------
# check options
# -----------------------------------------------------------------------
while ($ARGV[0] =~ /^-/) {
    if ($ARGV[0] =~ /v/) {$verbose = 1;}
    if ($ARGV[0] =~ /h/) {$helpmsg = 1;}
    shift(@ARGV);
    $nargin--;
    if ($nargin == 0) {
	last;
    }
}

# -----------------------------------------------------------------------
# help (if requested)
# -----------------------------------------------------------------------
if ($helpmsg) {
    print "\nREDS2PSAT converts REDS data files into PSAT data files.\n\n";
    print "reds2psat [options] fileinput [fileoutput]\n";
    print "  -v   verbose\n";
    print "  -h   print this help and exit\n\n";
    print "Author:   Federico Milano\n";
    print "Date:     07-May-2009\n";
    print "E-mail:   Federico.Milano\@uclm.es\n";
    die "\n";
}

# -----------------------------------------------------------------------
# define output file name (if necessary)
# -----------------------------------------------------------------------
if ($nargin == 1) {
    $ARGV[1] = $ARGV[0];
    $ARGV[1] =~ s/^d*_*/d_/;
    $ARGV[1] =~ s/[^\w\.]/_/g;
    $ARGV[1] =~ s/\..+$/.m/;
} elsif ($nargin == 0) {
    die "Error: Input file name is missing.\n";
}

# -----------------------------------------------------------------------
# open input data file
# -----------------------------------------------------------------------
print "Opening REDS data file \"$ARGV[0]\"...\n";
carret();
open(IN,$ARGV[0]) || die "cannot open $ARGV[0]: $!\n";

# -----------------------------------------------------------------------
# scan data file
# -----------------------------------------------------------------------

# read case study parameters
while (<IN>) {
    chomp;
    @data = split /\s+/;

    if (/^\.PU/) {

	$pu = $data[1];

    } elsif (/^\.Branch_count/) {

	$branch_count = $data[1];

    } elsif (/^\.Sectionalizing_count/) {

	$sec_count = $data[1];

    } elsif (/^\.Tie_count/) {

	$tie_count = $data[1];

    } elsif (/^\.Nodes/) {

	$nodes = $data[1];

    } elsif (/^\.Feeder_node_ids/) {

	for ($i = 1; $i <= @data; $i++) {
	    $feeders_ids[$i - 1] = $data[$i] + 1;
	}

    } elsif (/^\.Feeders/) {

	$feeders = $data[1];

    } elsif (/^\.Branch/) {

	while (<IN>) {
	    chomp;
	    if (/^\.Tie/) {
		$tie = 0;
		next;
	    }
	    if (/^\.Node/) { last; }
	    @data = split /\s+/;

	    $busfr[++$nline] = $data[1] + 1;
	    $bustt[$nline] = $data[2] + 1;

	    if ( $pu ) {
		$r[$nline] = $data[3];
		$x[$nline] = $data[4];
	    } else {
		$r[$nline] = $data[3]*$yb;
                $x[$nline] = $data[4]*$yb;
	    }

	    if ( $tie ) {
		$p = $data[5];
		$q = $data[6];
	    } else {
		$p = 0;
		$q = 0;
	    }

	    if ( $p != 0 || $q != 0 ) {
		$buspq[++$npq] = $data[2] + 1;
		$p[$npq] = $p;
		$q[$npq] = $q;
		$totp += $p;
	    }

	}
    }

}

# -----------------------------------------------------------------------
# close data file
# -----------------------------------------------------------------------
close(IN) || die "cannot close $ARGV[0]: $!\n";

# -----------------------------------------------------------------------
# open output data file
# -----------------------------------------------------------------------
print "Writing PSAT file \"$ARGV[1]\"...\n";
carret();
open(OUT,">$ARGV[1]") || die "cannot open $ARGV[1]: $!\n";

# -----------------------------------------------------------------------
# write output data file
# -----------------------------------------------------------------------
print OUT "% File generated by PSAT from REDS data file.\n";
print OUT "% "."-" x 78 . "\n";
print OUT "% Author:   Federico Milano\n";
print OUT "% E-mail:   Federico.Milano\@uclm.es\n";
print OUT "% "."-" x 78 . "\n";
print OUT "% 'Generic REDS data format "."$ARGV[0]'"."\n";

# -----------------------------------------------------------------------
# write Bus.con
# -----------------------------------------------------------------------
$format = "%4d  %8.5f  1.0  0.0  1  1;\n";
$nodes >= 0 && print OUT "Bus.con = [ ...\n";
for ($i = 1; $i <= $nodes; $i++) {
    printf OUT $format, $i, $vn;
}
$nodes >= 0 && print OUT "   ];\n\n";

# -----------------------------------------------------------------------
# write SW.con
# -----------------------------------------------------------------------
if ($feeders >= 0) {
    print OUT "SW.con = [ ...\n";
    $format = "%4d $mva $vn 1.0 0.0 99.9 -99.9 1.1 0.9 0.0 1 %d 1;\n";
    for ($i = 0; $i <= $feeders - 1; $i++) {
	$h = $feeders_ids[$i];
	if ( $i == 1 ) { $slack = 0; }
	printf OUT $format, $h, $slack;
    }
    print OUT "   ];\n\n";
}

# -----------------------------------------------------------------------
# write PQ.con
# -----------------------------------------------------------------------
$npq >= 0 && printf OUT "PQ.con = [ ...\n";
$format = "%4d $mva $vn %8.5f %8.5f 1.1 0.9 1 1;\n";
if ($totp/$nodes < 5) { $kva = 100; } # heuristic for defining power base
for ($i = 0; $i <= $npq; $i++) {
    printf OUT $format, $buspq[$i], $p[$i]/$kva, $q[$i]/$kva;
}
$npq >= 0 && print OUT "   ];\n\n";

# -----------------------------------------------------------------------
# write Line.con
# -----------------------------------------------------------------------
if ($nline >= 0) {
    print OUT "Line.con = [ ...\n";
    $format = "%4d %4d $mva $vn 60 0 0 %8.5f %8.5f 0 0 0 0 0 0 1;\n";
    for ($i = 0; $i <= $nline; $i++) {
	printf OUT $format, $busfr[$i], $bustt[$i], $r[$i], $x[$i];
    }
    print OUT "   ];\n\n";
}

# -----------------------------------------------------------------------
# write bus names
# -----------------------------------------------------------------------
$nodes >= 0 && print OUT "Bus.names = { ...\n";
$h = ($nodes + 1) % 5;
if ($h == 0) {$h = 5;}
if (($nodes + 1) > 5) {
    for ($i = 0; $i <= $nodes - $h; $i+=5) {
	$i1 = $i + 1;
	$i2 = $i1 + 1;
	$i3 = $i2 + 1;
	$i4 = $i3 + 1;
	print OUT qq(  'Bus $i'; 'Bus $i1'; 'Bus $i2'; 'Bus $i3'; 'Bus $i4';\n);
    }
}
print OUT "  ";
for ($i = $nodes - $h + 1; $i <= $nodes - 1; $i++) {
    print OUT "'Bus $i'; ";
}
print OUT "'Bus $nodes'};\n\n";

# -----------------------------------------------------------------------
# close output data file
# -----------------------------------------------------------------------
close(OUT) || die "cannot close $ARGV[1]: $!\n";
print "Conversion completed.\n";

# -----------------------------------------------------------------------
# function for writing a separator
# -----------------------------------------------------------------------
sub carret {
    $verbose && print "-" x 50 . "\n";
}